home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: allocating unlimited string input....HELP
- Date: 10 Jan 1996 14:38:19 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4d0j0r$7aq@news.iag.net>
- References: <4cvph6$s8s@pulp.ucs.ualberta.ca>
- NNTP-Posting-Host: pm1-orl23.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4cvph6$s8s@pulp.ucs.ualberta.ca>, ryangall@gpu.srv.ualberta.ca
- says...
- >
- >I am doing an assignment for my computing class, and seem to have run into
- >a small problem. I am supposed to be able to read an unlimited string from
- >stdin, without defining maximum size. I thought that this problem would be
- >simple....but I have been at it for a while now. Ill show you what my last
- >resort was.....
- >
- >
- >
- >int read(char *S)
- >{
- > char *A,*B;
- > char ch,count=0;
- >
- > A=NULL;
- > B=NULL;
- >
- > ch=getc(stdin);
- >
- > while(ch!='\n')
- > {
- > B=(char *) malloc(count+2);
- > if(count>0)
- > {
- > sprintf(B,"%s%c",A,ch);
- > free(A);
- > }
- > else sprintf(B,"%c",ch);
- >
- > printf("%s---%i----%i\n",B,strlen(B),count);
- > A=B;
- > ch=getc(stdin);
- > count=count+1;
- > }
- >
- >}
- >
- <snip>
- >Anyways, the function reads in just fine, but for some reason when it gets
- >to 146 characters it bails out, loses char *B and starts reducing
-
- Are you sure it doesn't start acting up, when count reaches 128? That's
- the result where char is a signed char. At 127 + 1, count goes negative
- (becomes -128). Subsequent loops would count up from there.
-
- Of course, the first malloc call with a negative size causes a failure, but
- your code isn't testing for allocation failure. So you don't catch it
- there. Instead you start using invalid pointers in your sprintf and free
- calls. From that point on anything can happen.
-
- You should probably see count go negative in the output from your printf.
- Unfortunately, your format string used '-' between the fields, so the
- negative sign simply blends in.
-
- So you need to:
-
- 1. define count as a larger type.
- 2. add a condition to your loop to ensure that count doesn't exceed
- the maximum for its type (look in limits.h for macros).
- 3. add an allocation failure test (if(B==NULL) {/* handle fail*/}).
- 4. modify the format string in your printf.
- 5. define ch as int (the correct return type for getc).
-
- You might want to:
- 1. loop into the realloc function. It is made for this type of code.
- 2. d/l and read the c.l.c faq (Frequently Asked Question) list. It is
- available for anonymous ftp from rtfm.mit.edu /pub/usenet/comp.lang.c.
- 3. remove the cast from the malloc return. It is not necessary in ansi
- c and can hide some errors.
- 4. add ch == EOF as a break condition for your loop.
-
- >please mail me ASAP....thanks!
-
- Sorry, I don't think I have a large enough envelope and I certainly can't
- afford the postage. However, I will email a copy of my reply to you :-)
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-